package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
import common.HttpClientUtil;
import entity.CroBuycars;
import entity.CroCustomer;
import entity.CroPurchase;
import entity.CroPurgoods;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroBuycarsService;
import service.CroPurchaseService;
import service.CroPurgoodsService;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chris on 17-5-13.
 */
public class UrlSearchAction extends ActionSupport {

    @Autowired
    private CroPurgoodsService croPurgoodsService;

    @Autowired
    private CroPurchaseService croPurchaseService;

    @Autowired
    private CroBuycarsService croBuycarsService;

    private String addurl;
    private String descHtml;
    private String jsonResult;
    private String reqUrl = "http://www.yoybuy.com/cn/getgoodsitem?goodsUrl=";

    private String title;
    private String purgoods;
    private String skuId;
    private String buycar;
    private String uniprice;
    private String totalprice;
    private String number;

    /**
     * 代购链接处理
     *
     * @return
     */
    public String searchurl() {
        try {
            if (addurl != null) {
                int point = addurl.indexOf("?");
                String arr[] = addurl.substring(point).split("&");
                addurl = addurl.substring(0, point + 1);
                String Id = "";
                for (String s : arr) {
                    if (s.contains("id")) {
                        Id = s.substring(s.indexOf("id"));
                        break;
                    }
                }
                addurl = addurl + Id;
                String ItemDetail = HttpClientUtil.httpGets(reqUrl + addurl);
                JSONObject Item = JSON.parseObject(ItemDetail);
                descHtml = Item.getJSONObject("data").getString("Desc");
                jsonResult = JSON.toJSONString(ItemDetail);
            }
        } catch (Exception e) {
            jsonResult = "ERROR";
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 代购物品添加至购物车
     *
     * @return
     */
    public String addToCar() {
        ServletContext context = ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");

        //通过客户查询未下单的订单
        CroPurchase croPurchase = croPurchaseService.FindPurchaseByCus(customer.getId());
        if (croPurchase == null) {
            //如果没有则创建新的
            CroPurchase tmpchase = new CroPurchase();
            tmpchase.setCroCustomer(customer);
            tmpchase.setExcessfee(0);
            tmpchase.setTotalprice(0);
            tmpchase.setOrderdate(new Date());
            tmpchase.setPaytype("0");
            tmpchase.setOrderStatus("0");
            tmpchase.setOrderType("0");
            tmpchase.setNotes("");
            tmpchase.setOrder_flag("0");
            String purchaseId = croPurchaseService.addPurchase(tmpchase);
            croPurchase = croPurchaseService.findById(purchaseId);
        }

        //辅助添加商品信息
        CroPurgoods croPurgoods = null;
        CroPurgoods tmpgoods = croPurgoodsService.getPurgoodsByName(title);
        if (tmpgoods == null) {
            tmpgoods = new CroPurgoods();
            tmpgoods.setType("9969");
            tmpgoods.setGoodname(title);
            tmpgoods.setDescription(purgoods);
            tmpgoods.setUniprice(Double.parseDouble(uniprice));
            tmpgoods.setGoodsnumber("300");
            String goodId = croPurgoodsService.addPurgoods(tmpgoods);
            croPurgoods = croPurgoodsService.getPurgoodsById(goodId);
        } else {
            croPurgoods = tmpgoods;
        }

        //购物车商品处理
        List<CroBuycars> buycarsList = croBuycarsService.listByPurchase(croPurchase);
        boolean hasgoods = false;
        int key = -1;
        for (int i = 0; i < buycarsList.size(); i++) {
            JSONObject carItem = JSON.parseObject(buycarsList.get(i).getGoodsdetail());
            System.out.println(carItem.getString("SkuId"));
            System.out.println(skuId);
            if (carItem.getString("SkuId").contentEquals(skuId)) {
                hasgoods = true;
                key = i;
            }
        }

        CroBuycars croBuycars = null;
        //添加购物车
        if (hasgoods) {
            croBuycars = buycarsList.get(key);
            croBuycars.setNumbers(croBuycars.getNumbers() + Integer.valueOf(number));
            croBuycars.setSumprice(croBuycars.getSumprice() + Double.parseDouble(totalprice));
        } else {
            croBuycars = new CroBuycars();
            croBuycars.setCroPurchase(croPurchase);
            croBuycars.setCroPurgoods(croPurgoods);
            croBuycars.setGoodsdetail(buycar);
            croBuycars.setNumbers(Integer.parseInt(number));
            croBuycars.setActualprice(Double.parseDouble(uniprice));
            croBuycars.setSumprice(Double.parseDouble(totalprice));
        }

        if (croBuycarsService.saveOrUpdateBuyCars(croBuycars)){
            jsonResult = "{\"status\":1,\"msg\":\"购物车添加成功!\"}";
        }else {
            jsonResult = "{\"status\":0,\"msg\":\"购物车添加失败!\"}";
        }

        return SUCCESS;
    }

    public String getAddurl() {
        return addurl;
    }

    public void setAddurl(String addurl) {
        this.addurl = addurl;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public String getDescHtml() {
        return descHtml;
    }

    public void setDescHtml(String descUrl) {
        this.descHtml = descHtml;
    }

    public String getReqUrl() {
        return reqUrl;
    }

    public void setReqUrl(String reqUrl) {
        this.reqUrl = reqUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPurgoods() {
        return purgoods;
    }

    public void setPurgoods(String purgoods) {
        this.purgoods = purgoods;
    }

    public String getSkuId() {
        return skuId;
    }

    public void setSkuId(String skuId) {
        this.skuId = skuId;
    }

    public String getBuycar() {
        return buycar;
    }

    public void setBuycar(String buycar) {
        this.buycar = buycar;
    }

    public String getUniprice() {
        return uniprice;
    }

    public void setUniprice(String uniprice) {
        this.uniprice = uniprice;
    }

    public String getTotalprice() {
        return totalprice;
    }

    public void setTotalprice(String totalprice) {
        this.totalprice = totalprice;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
